<?php

namespace App\Http\Controllers;


use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Tools\Func;
use Illuminate\Support\Facades\DB;

use App\Models\HeTong;

class HeTongController extends Controller
{
    use Func; 

    public function heTongView1()
    {
        return view('hetong1');
    }   

	public function login(Request $request)
	{
		$username = $request->username;	
		$password = $request->password;

		if (!$username) {
			return $this->returnJson(1, '账号不能为空', $request->all());		
		}

		if (!$password) {
			return $this->returnJson(1, '密码不能为空', $request->all());		
		}

		if (!$resHeTong= HeTong::where([
				'no' => $username,
				'password' => $password
		])->first()) {
			return $this->returnJson(1, '账号密码错误', $request->all());
			
		}
		return $this->returnJson(0, 'success', $resHeTong);
	}

    public function heTongView2(Request $request)
    {
        $id = $request->id;
        if ($res = HeTong::find($id)) {
            $no = $res->no;
            $res->sfz = substr($no, 0, 4) . '***********'.substr($no, strlen($no)-3, 3);
        }
        // dd($res);
        return view('hetong2', ['data' => $res]);
    }   

    public function heTongView3(Request $request)
    {
        $name = $request->name;
        $no = $request->no;
        $diqu = $request->diqu;
        $res = HeTong::where([
                'no' => $no,
                'name' => $name,
                'diqu' => $diqu
            ])->get();
        // echo count($res);exit;
        if (!count($res)){
            return $this->returnJson(1, '没找到数据');   
        }

        return $this->returnJson(0, 'success', $res);
    }   

    public function getHeTong(Request $request)
    {
        if (!$username = trim($request->username) ?? '') {
            return $this->returnJson(1, '账号不能为空', $request->all());    
        }
            if (!$password = trim($request->password) ?? '') {
            return $this->returnJson(1, '密码不能为空', $request->all());    
        }
        if (!$resHeTong= HeTong::where([
                'no' => $username
            ])->first()) {
            return $this->returnJson(1, '账号密码错误', $request->all());  
        }

        return $this->returnJson(0, 'success', $resHeTong);
    }
	
    public function getHeTongData(Request $request)
    {
        $no = $request->no;
        // if ($res = HeTong::where('no', $request->no)->get()) {
        //     $no = $res->no;
        //     $res->sfz = substr($no, 0, 4) . '***********'.substr($no, strlen($no)-3, 3);
        // }
        // dd($res);
		$res = HeTong::where('no', $request->no)->get();
        return $this->returnJson(0, 'success', $res);
    }   
	
	public function yongTu(Request $request)
	{
		$res = [
			[
				'value' => '单位住房调查',
				'text' => '单位住房调查',
			],
			[
				'value' => '征收(拆迁)调查',
				'text' => '征收(拆迁)调查',
			],
			[
				'value' => '公房出售',
				'text' => '公房出售',
			],
			[
				'value' => '户口外迁',
				'text' => '户口外迁',
			],
			[
				'value' => '办理低保',
				'text' => '办理低保',
			],
			[
				'value' => '有房证明',
				'text' => '有房证明',
			],
			[
				'value' => '无抵押证明',
				'text' => '无抵押证明',
			],
			[
				'value' => '无查封证明',
				'text' => '无查封证明',
			],
			[
				'value' => '异地房屋买卖',
				'text' => '异地房屋买卖',
			],
			[
				'value' => '异地住房贷款',
				'text' => '异地住房贷款',
			],
			[
				'value' => '子女入学',
				'text' => '子女入学',
			],
		];
		return $this->returnJson(0, 'success', $res);
	}
	
	public function diQu(Request $request)
	{
		$res = [
			[
				'value' => '城六区',
				'text' => '城六区',
			],
			[
				'value' => '长安区',
				'text' => '长安区',
			],
			[
				'value' => '高陵区',
				'text' => '高陵区',
			],
			[
				'value' => '临潼区',
				'text' => '临潼区',
			],
			[
				'value' => '阎良区',
				'text' => '阎良区',
			],
			[
				'value' => '鄠邑区',
				'text' => '鄠邑区',
			],
			[
				'value' => '蓝田县',
				'text' => '蓝田县',
			],
			[
				'value' => '周至县',
				'text' => '周至县',
			],
			[
				'value' => '西咸新区',
				'text' => '西咸新区',
			],
			[
				'value' => '港务区',
				'text' => '港务区',
			],
		];
		return $this->returnJson(0, 'success', $res);
	}

}
